import os
import csv
from tensorflow.keras.preprocessing import image
 
TRAIN_IMG_DIR = 'G:/doc7/Dogs vs Cats Redux Kernels Edition/train'
TRAIN_IMG_OUT_DIR = 'G:/doc7/Dogs vs Cats Redux Kernels Edition/train_out'
TRAIN_CSV_DIR = 'G:/doc7/Dogs vs Cats Redux Kernels Edition/train_labels.csv'
TEST_IMG_DIR = 'G:/doc7/Dogs vs Cats Redux Kernels Edition/test'
#统一图片大小
IMAGE_SIZE = 28
#生成CSV数据 图片文件名 0cat1dog
def mkcsv(img_dir, csv_dir):
    list = []
    list.append(['File Name','Label'])
    item=[]
    for file_name in os.listdir(img_dir):
        if file_name[0:3] =='cat':
            item =[file_name ,0]
        if file_name[0:3] =='dog':
            item =[file_name ,1]
        list.append(item)
    f = open(csv_dir, 'w', newline='')
    writer = csv.writer(f)
    writer.writerows(list)
#输出修改图片大小的图片数据
def format_img(input_dir, output_dir):
    for file_name in os.listdir(input_dir):
        path_name = os.path.join(input_dir, file_name)
        img = image.load_img(path_name, target_size=(IMAGE_SIZE, IMAGE_SIZE))
        path_name = os.path.join(output_dir, file_name)
        img.save(path_name)
#生成CSV
mkcsv(TRAIN_IMG_DIR, TRAIN_CSV_DIR)
#输出修改图片大小的图片数据
format_img(TRAIN_IMG_DIR, TRAIN_IMG_OUT_DIR)